package per.com.service.ServiceImpl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.google.common.collect.Lists;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import per.com.bean.Report;
import per.com.bean.User;
import per.com.common.ResponseCode;
import per.com.common.ServerResponse;
import per.com.dao.PcUsersDao;
import per.com.dao.ReportMapper;
import per.com.service.IReportService;
import per.com.utils.DateTimeUtil;
import per.com.utils.UUidUtils;
import per.com.vo.ReportDetailVo;
import per.com.vo.ReportListVo;

import java.util.List;

/**
 * @author zxy
 */
@Service("iReportService")
public class ReportServiceImpl implements IReportService {

    @Autowired
    private ReportMapper reportMapper;

    @Autowired
    private PcUsersDao pcUsersDao;

    @Override
    public ServerResponse<PageInfo> findAllReport(int pageNum, int pageSize) {

        PageHelper.startPage(pageNum , pageSize);
        List<Report> reportList = reportMapper.selectAllReport();
        List<ReportListVo> reportListVoList = Lists.newArrayList();
        for (Report report: reportList){
            ReportListVo reportListVo = assembleReportListVo(report);
            reportListVoList.add(reportListVo);
        }
        PageInfo pageInfo = new PageInfo(reportList);
        pageInfo.setList(reportListVoList);

        return ServerResponse.createBySuccess(pageInfo);
    }

    @Override
    public ServerResponse<PageInfo> findOneReport(String userId, int pageNum, int pageSize) {

        User user = pcUsersDao.selectByPrimaryKey(userId);
        if (user == null){
            return ServerResponse.createByErrorMessage("没有此用户，请检查传递的参数");
        }
        PageHelper.startPage(pageNum , pageSize);
        List<Report> reportList = reportMapper.selectUserReport(userId);
        if (CollectionUtils.isEmpty(reportList)){
            ServerResponse.createByErrorMessage("该用户没有举报过他人");
        }
        List<ReportListVo> reportListVoList = Lists.newArrayList();
        for (Report report: reportList){
            ReportListVo reportListVo = assembleReportListVo(report);
            reportListVoList.add(reportListVo);
        }
        PageInfo pageResult = new PageInfo(reportList);
        pageResult.setList(reportListVoList);

        return ServerResponse.createBySuccess(pageResult);
    }

    @Override
    public ServerResponse<PageInfo> findOneReported(String reportedId, int pageNum, int pageSize) {

        User user = pcUsersDao.selectByPrimaryKey(reportedId);
        if (user == null){
            return ServerResponse.createByErrorMessage("没有此用户，请检查传递的参数");
        }
        PageHelper.startPage(pageNum , pageSize);
        List<Report> reportList = reportMapper.selectReportedUser(reportedId);
        if (CollectionUtils.isEmpty(reportList)){
            return ServerResponse.createByErrorMessage("该用户没有被举报过");
        }
        List<ReportListVo> reportListVoList = Lists.newArrayList();
        for (Report report: reportList){
            ReportListVo reportListVo = assembleReportListVo(report);
            reportListVoList.add(reportListVo);
        }
        PageInfo pageInfo = new PageInfo(reportList);
        pageInfo.setList(reportListVoList);

        return ServerResponse.createBySuccess(pageInfo);
    }

    @Override
    public ServerResponse<ReportDetailVo> findReportDetail(String id) {

        Report report = reportMapper.selectByPrimaryKey(id);
        if (report == null){
            return ServerResponse.createByErrorCodeMessage(ResponseCode.ILLEGAL_ARGUMENT.getCode() , ResponseCode.ILLEGAL_ARGUMENT.getDesc());
        }
        ReportDetailVo reportDetailVo = assembleReportDetailVo(report);

        return ServerResponse.createBySuccess(reportDetailVo);
    }

    @Override
    public ServerResponse<ReportDetailVo> addReport(String userId , String reportReason, String reportedName) {
        if (StringUtils.isBlank(reportReason) || StringUtils.isBlank(reportedName)){
            return ServerResponse.createByErrorCodeMessage(ResponseCode.ILLEGAL_ARGUMENT.getCode() , ResponseCode.ILLEGAL_ARGUMENT.getDesc());
        }
        User user = pcUsersDao.selectUser(reportedName);
        Report report = new Report();
        report.setId(UUidUtils.getId());
        report.setUserId(userId);
        report.setReportedId(user.getUid());
        report.setReportReason(reportReason);
        int resultCount = reportMapper.insert(report);
        if (resultCount > 0){
            Report report1 = reportMapper.selectByPrimaryKey(report.getId());
            ReportDetailVo reportDetailVo = assembleReportDetailVo(report1);
            return ServerResponse.createBySuccess("举报成功" , reportDetailVo);
        }
        return ServerResponse.createByErrorMessage("举报失败");

    }

    private ReportDetailVo assembleReportDetailVo(Report report){
        ReportDetailVo reportDetailVo = new ReportDetailVo();
        reportDetailVo.setId(report.getId());
        reportDetailVo.setReportedId(report.getReportedId());
        reportDetailVo.setUserId(report.getUserId());
        reportDetailVo.setReportReason(report.getReportReason());
        User user = pcUsersDao.selectByPrimaryKey(report.getUserId());
        User reportUser = pcUsersDao.selectByPrimaryKey(report.getReportedId());
        reportDetailVo.setUsername(user.getUsername());
        reportDetailVo.setSex(user.getSex());
        reportDetailVo.setTel(user.getTel());
        reportDetailVo.setReportedUsername(reportUser.getUsername());
        reportDetailVo.setReportedSex(reportUser.getSex());
        reportDetailVo.setReportedTel(reportUser.getTel());
        reportDetailVo.setCreateTime(DateTimeUtil.dateToStr(report.getCreateTime()));
        reportDetailVo.setUpdateTime(DateTimeUtil.dateToStr(report.getUpdateTime()));

        return reportDetailVo;
    }

    private ReportListVo assembleReportListVo(Report report){
        ReportListVo reportListVo = new ReportListVo();
        reportListVo.setId(report.getId());
        reportListVo.setUserId(report.getUserId());
        reportListVo.setReportedId(report.getReportedId());
        User user = pcUsersDao.selectByPrimaryKey(report.getUserId());
        User reportUser = pcUsersDao.selectByPrimaryKey(report.getReportedId());
        reportListVo.setUsername(user.getUsername());
        reportListVo.setReportUsername(reportUser.getUsername());
        reportListVo.setCreateTime(DateTimeUtil.dateToStr(report.getCreateTime()));
        reportListVo.setUpdateTime(DateTimeUtil.dateToStr(report.getUpdateTime()));

        return reportListVo;
    }
}
